const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tstatus:true,

    ydpage:1,
    ydlimit:10,
    ydlist:[],
    ydname:'',


    // 推荐
    tjpage: 1,
    tjlimit: 15,
    tjlist: [],
    tjname: ''
  },
  // 跳转推荐阅读
  gorecommend:function(e){
    console.log(e);
  },
  ydinput:function(e){
    console.log(e)
    this.setData({
      ydname:e.detail.value,
      ydlist:[]
    })
    this.ydgetlist();
  },
  // 叉号清除name
  clearname:function(){
    this.setData({
      ydname:'',
      ydpage: 1,
      ydlist: [],
    })
    this.ydgetlist();
  },
  tj:function(){
    this.setData({
      tstatus: true,
      tjlist: [],
      tjpage: 1,
      tjname: ''
    })
    this.tjgetlist()
  },
  jl:function(){
    this.setData({
      tstatus: false,
      ydpage: 1,
      ydlist: [],
      ydname: '',
    })
    this.ydgetlist();
  },
  /**
   * 生命周期函数--监听页面加载
   */

  // 阅读记录
  ydgetlist: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        console.log(res);
        var setdata = {
          token: res.data.token,
          page: that.data.ydpage,
          limit: that.data.ydlimit,
          stuname:that.data.ydname,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.read_history,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  ydlist: e.data.data.list
                })
                console.log("aaa")
                wx.hideLoading();
                // 空的
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon:'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let ydlist = that.data.ydlist
                console.log(ydlist)
                ydlist = ydlist.concat(e.data.data.list)
                that.setData({
                  ydlist: ydlist
                })
                wx.hideLoading()
                console.log(ydlist)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon:'none',
                  duration: 2000
                })
              }

             
            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      },
    })
  },
  // 推荐阅读
  tjgetlist: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        console.log(res);
        var setdata = {
          token: res.data.token,
          page: that.data.tjpage,
          limit: that.data.tjlimit,
          stuname: that.data.tjname,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.student,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  tjlist: e.data.data.list
                })
                console.log("aaa")
                wx.hideLoading();
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon:'none',
                    duration: 2000
                  })
                }
              } else if (e.data.data.list.length) {
                let tjlist = that.data.tjlist
                tjlist = tjlist.concat(e.data.data.list)
                that.setData({
                  tjlist: tjlist
                })
                wx.hideLoading()
                console.log(tjlist)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icone:'none',
                  duration: 2000
                })
              }

              // 空的

            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      },
    })
  },
  onLoad: function (options) {
    wx.hideTabBar({})
    this.tjgetlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    if(that.data.tstatus){
      that.setData({
        tjpage: that.data.tjpage + 1,
      })
      this.tjgetlist()
    }else{
      that.setData({
        ydpage: that.data.ydpage + 1
      })
      this.ydgetlist()
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // tabbar
  goindex: function () {
    wx.reLaunch({
      url: '../index/index'
    })
  },
  gohs: function () {
    wx.reLaunch({
      url: '../hs/hs'
    })
  },
  gomine: function () {
    wx.reLaunch({
      url: '../mine/mine'
    })
  }
})